﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.MissionViews.MissionCameraFadeView
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.MissionViews
{
  [DefaultView]
  public class MissionCameraFadeView : MissionView
  {
    private bool _autoFadeIn;
    private float _fadeInTime = 0.5f;
    private float _blackTime = 0.25f;
    private float _fadeOutTime = 0.5f;
    private float _stateDuration;

    public float FadeAlpha { get; private set; }

    public MissionCameraFadeView.CameraFadeState FadeState { get; private set; }

    public bool IsCameraFading
    {
      get
      {
        return this.FadeState == MissionCameraFadeView.CameraFadeState.FadingIn || this.FadeState == MissionCameraFadeView.CameraFadeState.FadingOut;
      }
    }

    public bool HasCameraFadeOut => this.FadeState == MissionCameraFadeView.CameraFadeState.Black;

    public bool HasCameraFadeIn => this.FadeState == MissionCameraFadeView.CameraFadeState.White;

    public override void OnMissionScreenInitialize()
    {
      base.OnMissionScreenInitialize();
      this._stateDuration = 0.0f;
      this.FadeState = MissionCameraFadeView.CameraFadeState.White;
      this.FadeAlpha = 0.0f;
    }

    public override void OnMissionScreenTick(float dt)
    {
      base.OnMissionScreenTick(dt);
      if (this.Mission == null || !this.MissionScreen.IsMissionTickable)
        return;
      this.UpdateFadeState(dt);
    }

    protected void UpdateFadeState(float dt)
    {
      if (this.IsCameraFading)
      {
        this._stateDuration -= dt;
        if (this.FadeState == MissionCameraFadeView.CameraFadeState.FadingOut)
        {
          this.FadeAlpha = MathF.Min((float) (1.0 - (double) this._stateDuration / (double) this._fadeOutTime), 1f);
          if ((double) this._stateDuration >= 0.0)
            return;
          this._stateDuration = this._blackTime;
          this.FadeState = MissionCameraFadeView.CameraFadeState.Black;
        }
        else
        {
          if (this.FadeState != MissionCameraFadeView.CameraFadeState.FadingIn)
            return;
          this.FadeAlpha = MathF.Max(this._stateDuration / this._fadeInTime, 0.0f);
          if ((double) this._stateDuration >= 0.0)
            return;
          this._stateDuration = 0.0f;
          this.FadeState = MissionCameraFadeView.CameraFadeState.White;
        }
      }
      else
      {
        if (!this.HasCameraFadeOut || !this._autoFadeIn)
          return;
        this._stateDuration -= dt;
        if ((double) this._stateDuration >= 0.0)
          return;
        this._stateDuration = this._fadeInTime;
        this.FadeState = MissionCameraFadeView.CameraFadeState.FadingIn;
        this._autoFadeIn = false;
      }
    }

    public void BeginFadeOutAndIn(float fadeOutTime, float blackTime, float fadeInTime)
    {
      if (this.Mission == null || !this.MissionScreen.IsMissionTickable || this.FadeState != MissionCameraFadeView.CameraFadeState.White)
        return;
      this._autoFadeIn = true;
      this._fadeOutTime = MathF.Max(fadeOutTime, 1E-05f);
      this._blackTime = MathF.Max(blackTime, 1E-05f);
      this._fadeInTime = MathF.Max(fadeInTime, 1E-05f);
      this._stateDuration = fadeOutTime;
      this.FadeAlpha = 0.0f;
      this.FadeState = MissionCameraFadeView.CameraFadeState.FadingOut;
    }

    public void BeginFadeOut(float fadeOutTime)
    {
      if (this.Mission == null || !this.MissionScreen.IsMissionTickable || this.FadeState != MissionCameraFadeView.CameraFadeState.White)
        return;
      this._autoFadeIn = false;
      this._fadeOutTime = MathF.Max(fadeOutTime, 1E-05f);
      this._blackTime = 0.0f;
      this._fadeInTime = 0.0f;
      this._stateDuration = fadeOutTime;
      this.FadeAlpha = 0.0f;
      this.FadeState = MissionCameraFadeView.CameraFadeState.FadingOut;
    }

    public void BeginFadeIn(float fadeInTime)
    {
      if (this.Mission == null || !this.MissionScreen.IsMissionTickable || this.FadeState != MissionCameraFadeView.CameraFadeState.Black || this._autoFadeIn)
        return;
      this._fadeOutTime = 0.0f;
      this._blackTime = 0.0f;
      this._fadeInTime = MathF.Max(fadeInTime, 1E-05f);
      this._stateDuration = fadeInTime;
      this.FadeAlpha = 1f;
      this.FadeState = MissionCameraFadeView.CameraFadeState.FadingIn;
    }

    public enum CameraFadeState
    {
      White,
      FadingOut,
      Black,
      FadingIn,
    }
  }
}
